package com.kanshu.ksgb.fastread.doudou.module.personal.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVUserManager;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UpdateInfoEvent;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UserBean;
import com.kanshu.ksgb.fastread.doudou.module.personal.presenter.PersonCenterService;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * Created by xubo on 2018/11/19
 */
@Route(path = ARouterConfig.PERSONAL_READ_PREFERENCES)
public class ReadPreferencesActivity extends BaseActivity {

    private boolean mIsBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_preferences);
        ButterKnife.bind(this);
        mIsBack = getIntent().getBooleanExtra("isBack", false);
        hideTitlebar();
    }

    @OnClick({R.id.sex_boy, R.id.sex_gril, R.id.sex_skip})
    public void onClick(View v){
        String sex = Constants.TYPE_SECRET;
        switch (v.getId()){
            case R.id.sex_boy:
                sex = Constants.TYPE_MALE;
                break;
            case R.id.sex_gril:
                sex = Constants.TYPE_FEMALE;
                break;
            case R.id.sex_skip:
                sex = Constants.TYPE_SECRET;
                break;
        }
        modifySex(sex);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return mIsBack || super.onKeyDown(keyCode, event);
    }

    private void modifySex(String sex){
        RetrofitHelper.getInstance()
                .createService(PersonCenterService.class)
                .modifySex(sex).compose(asyncRequest()).subscribe(new BaseObserver<UserBean>() {
                    @Override
                    public void onResponse(BaseResult<UserBean> result, UserBean userInfo, Disposable disposable) {
                        Log.d("modifySex", "onResponse  " + result.result.status.msg);
                        if(userInfo != null){
                            MMKVUserManager.getInstance().setLoginSex(userInfo.sex);
                            EventBus.getDefault().post(new UpdateInfoEvent());
                        }
                    }

                    @Override
                    public void onError(int code, String errDesc) {
                        super.onError(code, errDesc);
                        Log.d("modifySex", "onError  " + errDesc);
                    }
                });
        finish();
    }

    public static void actionStart(Context context, boolean isBack){
        Intent intent = new Intent(context, ReadPreferencesActivity.class);
        intent.putExtra("isBack", isBack);
        context.startActivity(intent);
    }
}
